package com.sunny.l3;

import java.util.HashMap;
import java.util.Map;

public class Solution {

    private Map<Double, Integer> res = new HashMap<>();
    private Map<Point, Integer> prec = new HashMap<>();

    public int maxPoints(Point[] points) {

        int result = 0;

        for (int i = 0; i < points.length; i++){
            if(null == points[i])
                continue;
            prec.put(points[i], 1);
            for(int j = i+1; j < points.length; j++){
                if(null == points[j])
                    continue;
                if(points[i].x == points[j].x && points[i].y == points[j].y){
                    //点重合的情况
                    prec.put(points[i], prec.get(points[i]) + 1);
                    points[j] = null;
                }
            }
        }

        for(int i=0;i<points.length;i++){
            if(null == points[i])
                continue;
            if(prec.get(points[i]) > result)
                result = prec.get(points[i]);
        }

        for(int i=0;i<points.length;i++){
            if(null == points[i])
                continue;
            for(int j=i+1;j<points.length;j++){
                if(null == points[j])
                    continue;
                int tmp = prec.get(points[i]) + prec.get(points[j]);
                if(tmp > result)
                    result = tmp;
            }
        }

        for (int i = 0; i < points.length; i++){
            if(null == points[i])
                continue;
            res.clear();
            int tmpResult = 0;
            for(int j = 0;j< points.length; j++){
                if(null == points[j] || i == j)
                    continue;
                double tmpSlope = getSlope(points[i], points[j]);
                if(res.containsKey(tmpSlope)){
                    res.put(tmpSlope, res.get(tmpSlope) + prec.get(points[j]));
                }else{
                    res.put(tmpSlope, prec.get(points[i]) + prec.get(points[j]));
                }
            }
            for(Map.Entry<Double,Integer> entry: res.entrySet()){
                if(entry.getValue() > tmpResult){
                    tmpResult = entry.getValue();
                }
            }
            if(tmpResult > result){
                result = tmpResult;
            }
        }
        return  result;
    }

    public double getSlope(Point a, Point b){
        return 1.0*(b.y-a.y)/(b.x-a.x);
    }

    public static void main(String[] args) {

        Point[] points = {new Point(0,0),new Point(1,1),new Point(0,0)};


        System.out.println(new Solution().maxPoints(points));

    }

}


class Point {
    int x;
    int y;
    Point() { x = 0; y = 0; }
    Point(int a, int b) { x = a; y = b; }
}